package leoner.multithread2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

public class MockServiceImpl implements MockService {
	private static Random generator = new Random();

	@Override
	public String m1(Integer id, String name) {
		System.out.println("m1 invoke");
		try {
			Thread.sleep(generator.nextInt(5000));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "id:" + id + ",name:" + name;
	}

	@Override
	public Integer m2(Integer value) {
		System.out.println("m2 invoke");
		try {
			Thread.sleep(generator.nextInt(5000));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return value;
	}

	@Override
	public MockBean m3(Integer id, Integer age, String name) {
		System.out.println("m3 invoke");
		try {
			Thread.sleep(generator.nextInt(5000));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MockBean bean = new MockBean();
		bean.setName(name);
		bean.setId(id);
		bean.setAge(age);
		return bean;
	}

	private static void getUrl(String urlString) {
		try {
			URL url = new URL(urlString);
			URLConnection yc = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc
					.getInputStream()));
			String inputLine;

			while ((inputLine = in.readLine()) != null){
				
			}
				//System.out.println(inputLine);
			in.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		getUrl("http://www.google.com");
		getUrl("http://news.sina.com.cn");
		System.out.println("done");
	}

}
